package com.frank.asynService.service;

import com.frank.redis.util.RedisLockUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author 小石潭记
 * @date 2020/10/27 11:47
 * @Description: ${todo}
 */
@Service
@Slf4j
public class ScheduleService {

    @Autowired
    private RedisLockUtil redisLock;

//    @Scheduled(cron = "0/10 * * * * ? ")
    public void sayHello(){
        String key = "dec_store_lock";
        long time = System.currentTimeMillis();
        try {
            //如果加锁失败
            if (!redisLock.tryLock(key, String.valueOf(time))) {
                return;
            }
            // 执行真正的业务操作
            log.info("当前线程{}，定时任务执行，{}", Thread.currentThread().getName(), "hello world");
        } catch (Exception e) {
            //解锁 异常
            redisLock.unlock(key, String.valueOf(time));
            return;
        }

    }

}
